extends Resource
class_name Npc_definition

var prefix="" # TODO remove
export(String) var type 
export(String) var variant
export(int) var money_max = 1800
export(float) var accepts_markup_buy = 1.3
export(float) var accepts_markup_sell = 0.4
export(float) var haggle_chance_max = 1.0 # 1.0 adds 100% haggle chance when the markup is at the max accepted price
export(float) var haggle_chance_min = 0.0005 # 0.0005 adds 0.05% haggle chance when the markup is at the base price, lower than base price means no haggle, set above 1.0 to have an npc always haggle
export(Dictionary) var visit_times = {"early_morning": 0.06, "morning": 1.0, "midday":1.0, "noon":1.0, "evening": 1.0, "night":0.05, "late_night":0.0} # multiplier how likely an npc will spawn during time
export(Dictionary) var visit_times_for_order =  {"early_morning": 0.7, "morning": 1.0, "midday":1.0, "noon":1.0, "evening": 1.0, "night":0.6, "late_night":0.01} # when they placed an order
#export(float) var spawn_chance = 0.0 # how likely this npc will appear if there is nothing enticing in the window
#export(int) var min_level = 0 # level you have to be on for this npc to spawn
#export(int) var min_day = 0 # days that must have passed for this npc to spawn
export(PoolStringArray) var min_achievements = ["level_0"] # achievements you must have on this save for this npc to spawn
export(bool) var unique = false # Will only spawn one of this type
export(Dictionary) var name_pool = {
	"en": {
		"first": ["Arachno", "Purin", "Puyo", "Thea", "Aria", "Tear", "Crebecca", "Albus", "Toni"],
		"last": ["Something", "Whatever", "Puyo", "Margatroid", "Iridescent", "Crab", "Blade", "Emery", "Onio"]
	}
}
export(Dictionary) var lines = {
	"en": {
		"greeting": "Hello shopkeeper",
		"buy": "How much is %item_name",
		"sell": "I want to sell %item_name",
		"request_category": "can i get some %category",
		"request_adjective": "uhh do you have something %adjective",
		"request_category_adjective": "uhh do you have %category that is %adjective",
		"request_matched_nothing": "If you don't have anything, I'll leave.",
		"request_matched_bad": "That is not the item I want.",
		"request_matched_okay": "Guess that will do.",
		"request_matched_good": "That's great!",
		"request_matched_perfect": "Yes this item is perfect!",
		"buy_price_outrage": "wtf are these prices!?!?! I leave!!!",
		"buy_price_much": "that's way too much",
		"buy_price": "that costs too much",
		"buy_price_little": "a bit cheaper pls",
		"buy_price_good": "Great price! I accept.",
		"buy_price_good.1": "Yay!",
		"buy_price_good.2": "Okey dokey!",
		"buy_price_good_much": "What a steal lol",
		"sell_price_outrage": "you are scamming me!!! I won't sell here!!",
		"sell_price_much": "I can get waaaay more than that!",
		"sell_price": "I want more money!",
		"sell_price.1": "Please raise the price.",
		"sell_price_little": "Just a bit more please",
		"sell_price_good": "Thanks for the money :)",
		"sell_price_good_much": "I'M RICH MUAHAHAAHAA",
		"out_of_money": "I can't afford that."
	}
}
export(Array) var desires = [
	# This NPC will spawn if a matching item is in the window, or spawn to request a matching item.
	{
		"request_chance": 0.1,
		"window_attraction": 0.4,
		"id": "",
		"category": "food",
		"adjectives": ["sweet", "soft"]
	},
	{
		"request_chance": 0.0,
		"window_attraction": 1.0,
		"id": "legendary_sword",
		"category": "",
		"adjectives": []
	},
	{
		"request_chance": 0.04,
		"window_attraction": 0.6,
		"id": "",
		"category": "armor",
		"adjectives": ["light"]
	}
]

func random_spawn(window_items : Array, achievements : Array):
	var chances = spawn_chance(window_items, achievements)
	for chance in chances:
		var f = randf()
		if f < chance.chance:
			return [self, chance]
	return null
	
class Visit_reason_item:
	var type = "buy"
	var item = null
	var chance : float
	func _init(item_arg, chance_arg):
		item = item_arg
		chance = chance_arg
class Visit_reason_request:
	var type = "request"
	var request: Dictionary
	var chance : float
	func _init(request_arg, chance_arg):
		request = request_arg
		chance = chance_arg
	
func spawn_chance(window_items, achievements):
	var reasons = []
	for a in min_achievements:
		if not achievements.has(a):
			return [] # does not have achievement required
	for d in desires:
		var window_attraction = d["window_attraction"]
		if window_attraction > 0.0:
			for item in window_items:
				if item.category == d["category"]:
					reasons.append(Visit_reason_item.new(item, window_attraction))
		var request_chance = d["request_chance"]
		if request_chance > 0.0:
			reasons.append(Visit_reason_request.new(d, request_chance))
	return reasons
